<?php
namespace app\m\controller;

use app\common\controller\FrontController;
use app\common\service\MemberService;

class Member extends FrontController
{
    public function info()
    {
        $memberService = new MemberService();
        if(request()->isAjax()){
            $param = input('param.');
            $jieguo = $memberService->setMemberInfo($this->getMemberId(),$param);
            if($jieguo['err_code'] != 0){
                $this->error($jieguo['msg']);
            }
            else{
                $this->success('成功');
            }
        }

        $jieguo = $memberService->getMemberInfo($this->getMemberId());
        if($jieguo['err_code'] != 0){
            $this->error($jieguo['msg']);
        }
        $memberModel = $jieguo['data'];

        $this->assign('memberModel', $memberModel);
        return $this->fetch();
    }

    public function cart()
    {
        if(request()->isAjax()){
            $memberService = new MemberService();
            $jieguo = $memberService->getMemberCartByMemberId($this->getMemberId());
            if($jieguo['err_code'] != 0){
                $this->error($jieguo['msg']);
            }
            else{
                $this->success('获取列表成功','',$jieguo['data']);
            }
        }

        return $this->fetch();
    }
    public function address_selector()
    {
        $selectId = input('select_id',0);
        $this->assign('nowSelect', $selectId);
        return $this->fetch();
    }

    public function address_post()
    {
        $id = input('id',0);
        $this->assign('isModfiy', $id > 0);
        if (!empty($id)) {
            $memberService = new MemberService();
            $jieguo = $memberService->getAddress($id);
            if($jieguo['err_code'] != 0){
                $this->error($jieguo['msg']);
            }
            // 处理 所在地区
            $address = $jieguo['data'];
            $address['areas'] = $address['province'] . ' ' . $address['city'] . ' ' . $address['area'];
            $this->assign('model',$address);
        }
        return $this->fetch();
    }
    public function address_delete()
    {
        $param = input('param.');
        $addressId = input('id',0);
        $memberService = new MemberService();
        $jieguo = $memberService->addressDelete($addressId,$this->getMemberId());
        if($jieguo['err_code'] != 0){
            $this->error($jieguo['msg']);
        }
        else{
            $this->success('删除成功');
        }
    }
    public function address_submit()
    {
        $param = input('param.');
        $memberService = new MemberService();
        $jieguo = $memberService->addressChange($param,$this->getMemberId());
        if($jieguo['err_code'] != 0){
            $this->error($jieguo['msg']);
        }
        else{
            $this->success('提交成功','',['addressid' => $jieguo['data']['id']]);
        }
    }

    public function get_address_list()
    {
        $param = input('param.');
        $keyword = input('keyword','');
        $memberService = new MemberService();
        $jieguo = $memberService->getAddressList($this->getMemberId(),$keyword);
        if ($jieguo['err_code'] != 0) {
            $this->error($jieguo['msg']);
        }
        $fanhui = [];
        // 数据再加工
        foreach ($jieguo['data'] as $item){
            $fanhui[] = [
                'id' => $item['id'],
                'is_default' => $item['is_default'],
                'realname' => $item['realname'],
                'mobile' => $item['mobile'],
                'address' => $item['province'] . $item['city'] . $item['area'] . ' ' . $item['address'],
                'edit_url' => url('address_post',['id'=>$item['id']]),
            ];
        }
        $this->success('成功', '', $fanhui);
    }

    public function address()
    {
        $selectId = input('select_id',0);
        $this->assign('nowSelect', $selectId);
        return $this->fetch();
    }

    public function address_set_default()
    {
        $param = input('param.');
        $addressId = input('id',0);
        $memberService = new MemberService();
        $jieguo = $memberService->addressSetDefault($addressId,$this->getMemberId());
        if($jieguo['err_code'] != 0){
            $this->error($jieguo['msg']);
        }
        else{
            $this->success('删除成功');
        }
    }

    /**
     * 更换绑定手机号
     */
    public function bind(){
        $memberService = new MemberService();
        if(request()->isAjax()){
            $param = input('param.');
            $jieguo = $memberService->bindMember($this->getProjectId(),$this->getMemberId(),$param);
            $jieguo['code'] = $jieguo['err_code'];
            unset($jieguo['err_code']);
            return $jieguo;
        }
        $jieguo = $memberService->getMemberInfo($this->getMemberId());
        if($jieguo['err_code'] != 0){
            $this->error($jieguo['msg']);
        }
        $memberModel = $jieguo['data'];

        $this->assign('memberModel', $memberModel);
        return $this->fetch();
    }

    public function change_pwd(){
        $memberService = new MemberService();
        if(request()->isAjax()){
            $param = input('param.');
            $jieguo = $memberService->changeMemberPwd($this->getProjectId(),$this->getMemberId(),$param);
            $jieguo['code'] = $jieguo['err_code'];
            unset($jieguo['err_code']);
            return $jieguo;
        }

        $jieguo = $memberService->getMemberInfo($this->getMemberId());
        if($jieguo['err_code'] != 0){
            $this->error($jieguo['msg']);
        }
        $memberModel = $jieguo['data'];

        $this->assign('memberModel', $memberModel);
        return $this->fetch();
    }
}
